package action.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.MessagesBiz;
import biz.impl.MessagesBizImpl;
import entity.Messages;


public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessagesBiz mb= new MessagesBizImpl();
		if(request.getParameter("username")!=null &&
				request.getParameter("email")!=null &&
				request.getParameter("phone")!=null &&
				request.getParameter("content")!=null){
			Messages message = new Messages(request.getParameter("username"),
					request.getParameter("email"),
					request.getParameter("phone"),
					request.getParameter("content"),
					new Date(),
					1);
			mb.addMessage(message);
			response.sendRedirect("MessageServlet");
		}
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int pageNum=1;
		int pageSize=3;
		if(request.getParameter("pageNum")!=null){
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}
		MessagesBiz mb1= new  MessagesBizImpl();
		List<Messages> messageslist= mb1.getMessage(pageNum, pageSize);
		int total=mb1.getAllMessage();
		int totalpage=total%pageSize==0?total/pageSize:total/pageSize+1;
		
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("messageslist", messageslist);
		
		
		request.getRequestDispatcher("message.jsp").forward(request, response);
	}

}
